﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using SocialGame.Models;
using SocialGame.Business;
using Social.Game.DAL;
using Ext.Net.MVC;
namespace SocialGame.Areas.Games.Controllers
{
    public class Question_TypeController : Controller
    {
        public ActionResult Index()
        {
            List<QuestionTypeModel> listTypes = new List<QuestionTypeModel>();
            var question = DbUtil.Database.Query<QuestionType>("SELECT * FROM QuestionType");
            int id = 0;
            foreach (var q in question.ToArray())
            {
             
                QuestionTypeModel model = new QuestionTypeModel();
                model.QuestionName = q.QuestionName;
                model.QuestionType = q._QuestionType;
              
                listTypes.Add(model);
            }
            return View(listTypes);
        }

        public ActionResult HandleChanges(StoreDataHandler handler)
        {
            List<QuestionTypeModel> persons = handler.ObjectData<QuestionTypeModel>();
           
            if (handler.Action == StoreAction.Create)
            {
                foreach (QuestionTypeModel created in persons)
                {
                    QuestionType type = new QuestionType()
                    {
                        _QuestionType = created.QuestionType,
                        QuestionName = created.QuestionName
                    };
                    DbUtil.Database.Insert(type);
                }
            }
            else if (handler.Action == StoreAction.Destroy)
            {
                foreach (QuestionTypeModel created in persons)
                {
                    QuestionType type = new QuestionType()
                    {
                        _QuestionType = created.QuestionType,
                        QuestionName = created.QuestionName
                    };
                    DbUtil.Database.Delete(type);
                }
            }
            else if (handler.Action == StoreAction.Update)
            {
                foreach (QuestionTypeModel created in persons)
                {
                    QuestionType type = new QuestionType()
                    {
                        _QuestionType = created.QuestionType,
                        QuestionName = created.QuestionName
                    };
                    DbUtil.Database.Update(type);
                }
            }

            return handler.Action != StoreAction.Destroy ? (ActionResult)this.Store(persons) : (ActionResult)this.Content("");
        }

    }
}
